<?php
// $Id$
/**
 * @file
 *
 * How to use it:
 *
 * ================================================================================================================
 * DON'T PLACE ON YOUR hook_init() We had a issue with the meta_tags module, include this .inc only when you use it
 * ================================================================================================================
 *
 * Example:
 *  function baseproject_init() {
 *    module_load_include('inc', 'baseproject', 'inc/special_replace');
 *  }
 *
 * Create a new filter on hook_filter() using _special_replace_filter_process($text); and _flash_replace_preprocess($text); on 'process'
 * Example:
    function baseproject_filter($op, $delta = 0, $format = -1, $text = '') {
      if($op == 'list') {
        return array(0 => t('Specials Replace'));
      }
      switch ($delta) {
        case 0:
        module_load_include('inc', 'baseproject', 'inc/special_replace');
          switch ($op) {
            case 'description':
              return _special_replace_filter_description();
            case 'prepare':
             return $text;
            case 'process':
              _special_replace_filter_process($text);
              _flash_replace_preprocess($text);
              return $text;
            default:
              return $text;
          }
          break;
      }
    }
 *
 * Create a new hook_menu() for the form_settings
 *   Example:
      function baseproject_menu() {
        $items['admin/settings/special-replaces'] = array(
          'title' => t('Special Replaces Configuration'),
          'page callback' => 'drupal_get_form',
          'page arguments' => array('baseproject_special_replace_admin_form'),
          'access arguments' => array('administer site configuration'),
        );
        return $items;
      }
 *
 * Create a new hook_form() for the baseproject_special_replace_admin_form() called on the hook_menu
 *   Example:
      function baseproject_special_replace_admin_form($form_state) {
        $form = array();
        module_load_include('inc', 'baseproject', 'inc/special_replace');
        _replace_specials_settings($form);
        return system_settings_form($form);
      }
 *
 * Place on your hook_preprocess()
 * Example:
    function baseproject_preprocess(&$variables, $hook) {
      switch ($hook) {
        case 'block':
          module_load_include('inc', 'baseproject', 'inc/special_replace');
          _special_replace_preprocess($variables['block']->content);
        break;
      }
    }
 *
 * Go to Site configuration > Input Formats, click on the "Configure" option of the selected input format and enable the created filter.
 *
 * You can also customize the replace_specials_sup() function to add a class to the sup tag or use of &trade; instead of <sup>TM</sup>, for instance.
 *
 *  Responsabilities:
 *  - replace @TM@ and @REG@ for trademark and registered signs in a standardized way
 */
 
/**
 * Create the settings for replace specials
 */
function _replace_specials_settings(&$form) {
  $form['replace_speciald'] = array(
    '#type' => 'fieldset',
    '#title' => t('Replace Specials Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE
  );
  $form['replace_speciald']['enable_block_replace'] = array(
    '#type' => 'checkbox',
    '#title' => t('Block Replace'),
    '#default_value' => variable_get('enable_block_replace', FALSE),
    '#description' => t('If checked, replace the specials chars in the blocks'),
  );
  //replace flash 
  $form['replace_flash'] = array(
    '#type' => 'fieldset',
    '#title' => t('Flash Replace'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['replace_flash']['flash_replace'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Flash Replace'),
    '#default_value' => variable_get('flash_replace', FALSE),
  );
  $form['replace_flash']['base_flash'] = array(
    '#type' => 'textfield',
    '#title' => t('Set path of files'),
    '#default_value' => variable_get('base_flash', ''),
    '#description' => t('Make sure this path ends with \'/\'. If you want flash base to simply be @base_path@ then leave this field blank.'),
  );
  $form['replace_flash']['info'] = array(
    '#type' => 'markup',
    '#value' => t('<p>Use: @[#{flashvar}#{attributes}#{flash name}#{div name}#"width","height"]@</p>
      <p>Leave blank those parameters that are not being used</p>
      <p>Example @[#{}#{}#Main.swf#flash-home#"500","300"]@</p>'),
  );
}

// function to be used in the filter module

function _special_replace_filter_list(&$list = array()) {
  $list[] = t('Specials Replace');
}
function _special_replace_filter_description() {
  return t('Allows the use of custom paths for url replacements (@REG@, @TM@, @[#{flashvar}#{attributes}#{flash name}#{div name}#"width","height"]@');
}
function _special_replace_filter_process(&$text) {
  $text = _specials_replace_title($text);
}

function _special_replace_preprocess(&$block_content) {
  //reaplce the special inside a block
  if (variable_get('enable_block_replace', FALSE)) {
    $block_content = _specials_replace_title($block_content);
  }
}

//function to be used in the hook_preprocess

/**
 * Reaplce the reg in the text, but prevents replace any reg inside a tag
 * @param $text Text to be replaced
 */
function _specials_replace_title($text) {
  $text =  preg_replace_callback('/>[^<]*</', 'replace_specials_sup', $text);
  return  preg_replace_callback('/\=\"[^"]*"/', 'replace_specials', $text);
}

/**
 * Function used like callback in the _reg_replace_title()
 * @param $matches All matches found by preg_replace_callback()
 */
function replace_specials($matches) {
  //replace both iten responsible for reg char
  $matches[0] = str_ireplace(array('@TM@', '@REG@'), array('&trade;', '&reg;'), $matches[0]);
  return $matches[0];
}

function replace_specials_sup($matches) {
  //replace both iten responsible for reg char
  $matches[0] = str_ireplace(array('@TM@', '@REG@'), array('<sup class="trade-sup">TM</sup>', '<sup class="reg-sup">&reg;</sup>'), $matches[0]);
  return $matches[0];
}

/*
 * Flash Replace
 */
function _flash_replace_content($text) {
  $text =  preg_replace_callback('/@\[([^\]@]*)\]@/', 'replace_flash', $text);
  return $text;
}

function replace_flash($matches) {
  $flash = explode("#", $matches[1]);
  $swf_object  = "<script type='text/javascript'>";
  $swf_object .= "var flashvars  = ". $flash[1] .";";
  $swf_object .= "var params = {base:\"". base_path() . variable_get('base_flash', '') ."\", allowScriptAccess:\"sameDomain\", allowFullScreen:\"false\", quality:\"high\", background:\"#FFFFFF\", wmode:\"transparent\"};";
  $swf_object .= "var attributes = ". $flash[2] .";";
  $swf_object .= "swfobject.embedSWF('". base_path() . variable_get('base_flash', '') . $flash[3] ."','". $flash[4] ."',". $flash[5] .", '10.0.0','". base_path() . variable_get('base_flash', '') ."expressInstall.swf', flashvars, params, attributes)";
  $swf_object .= "</script>";
  return $swf_object;
}

function _flash_replace_preprocess(&$block_content) {
  //replace the special inside a block
  if (variable_get('flash_replace', FALSE)) {
    $block_content = _flash_replace_content($block_content);
  }
}